// pages/profille/profile.js
const app = getApp()
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status_height: app.globalData.statubar_height,
    userInfo: {},
    userVideo: [],
    // 联系方式
    userConnect: {
        wx:null,
        wb:null,
        qq:null
    }, 
    //抖音联系方式
    dyconnect:null,
    copy_status: {
      wx_copied: false,
      dy_copied: false,
      wb_copied: false,
      qq_copied: false,
    },
    // 是否已关注
    isFocus: null,
    //联系方式类型
    type: null,
    flag:0
  },

  back: function(e){
    wx.navigateBack({
      //
    })
  },

  //加载用户信息
  getUserInfo:function(param){
      console.log(9090)
      console.log(param)
    var that = this 
    util.request('/douyin/getHomePageByDouYinNo',param,function(res){
      console.log(res)
      that.setData({
        userInfo: res
      })

      that.getUserVideo(param)
    })
  },
  
  //加载用户联系方式
  getUserConnect: function(param) {
      var that = this
      var par = param.currentTarget.dataset.connecttype
      var info = param.currentTarget.dataset.info
      util.request('/douyin/getSocialContactInfo', { 'type': par,'douyinUserNo':info},function(res) {
          switch(par){
              case 'wx':
                  that.setData({
                      ['userConnect.wx']: res
                  });
                  break;
                case 'wb':
                  that.setData({
                      ['userConnect.wb']: res
                  });
                  break;
                case 'qq':
                  that.setData({
                      ['userConnect.qq']: res
                  });
                  break;
          }
       
      })   
  },

  //加载用户视频
  getUserVideo: function(param){
    var that = this
    util.request('/douyin/queryVideoListByDouYinNo',param,function(res){
      console.log(res)
      that.setData({
        userVideo: res
      })
    })
  },

  // 复制联系方式
  copy: function (e) {
    console.log('copy')
      console.log(e.currentTarget.dataset.type)
      if (e.currentTarget.dataset.type !== 'dy_copied'){
          var key = e.currentTarget.dataset.connecttype
          var data = e.currentTarget.dataset.connectinfo[key]
          let type = e.currentTarget.dataset.type
          let that = this
          let copy_statu = {
              wx_copied: false,
              wb_copied: false,
              qq_copied: false,
          }

          wx.setClipboardData({
              data: data,
              success: function (res) {
                  copy_statu[type] = true
                  that.setData({
                      copy_status: copy_statu,
                  })
              }
          })
      } else {
          let copy_statu = {
              dy_copied: false,
          }
          let that = this
          let data = e.currentTarget.dataset.contact
          let type = e.currentTarget.dataset.type
          wx.setClipboardData({
              data: data,
              success: function (res) {
                  copy_statu[type] = true
                  that.setData({
                      copy_status: copy_statu,
                  })
              }
          })
      }
  },

  //跳转播放
  play: function (e) {
    let dyUserNo = e.currentTarget.dataset.userno

    wx.navigateTo({
      url: '../player/player?userNo=' + dyUserNo,
    })
  },

  //添加收藏
  addFav: function (e) {
    var isFav = e.currentTarget.dataset.isfav
    var that = this
    var param = {
      userNo: app.globalData.userInfo.userNo,
      douyinUid: e.currentTarget.dataset.dyno
    }
    // 点击关注之后改变对应的值
    /*  console.log(that.isFocus)
    that.setData({
        isFocus:!that.isFocus
    })*/
    if (isFav == '1') {
      util.request('/douyin/cancelCollectUser', param, function (res) {
        that.data.userInfo['isCollect'] = 0
        that.setData({
          userInfo: that.data.userInfo,
           isFocus: false
        })
      })
    } else {
      util.request('/douyin/collectUser', param, function (res) {
        that.data.userInfo['isCollect'] = 1
        that.setData({
          userInfo: that.data.userInfo,
          isFocus: true
        })
      })
    }

  },
  /*点击两次效果*/
  test: function(e) {
      let that = this
      let key = e.currentTarget.dataset.connecttype
      if(key !== undefined) {
          if (e.currentTarget.dataset.connectinfo[key] == null) {
              that.getUserConnect(e)
          } else {
              that.copy(e)
          }
      } else {
          let that = this
          let data = that.data.userInfo.douyinUid
          if (that.data.dyconnect == null) {
              that.setData({
                  dyconnect :data
              })
          }else {
              that.copy(e)
          }
      }
     
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log('options')
      console.log(options)
      this.getUserInfo(JSON.parse(options.data))
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})